package hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

public class CatDemo_0010 {

    public static void main(String[] args) throws IOException {
        // 创建Configuration对象
        Configuration conf = new Configuration();
        // 创建FileSystem对象
        FileSystem fs = FileSystem.get(URI.create(args[0]), conf);
        // 需求：查看/user/kevin/passwd的内容
        // args[0] hdfs://1.0.0.5:9000/user/zyh/passwd
        // args[0] file:///etc/passwd
        FSDataInputStream is = fs.open(new Path(args[0]));
        byte[] buff = new byte[1024];
        int length = 0;
        while ((length = is.read(buff)) != -1) {
            System.out.println(new String(buff, 0, length));
        }
        System.out.println(fs.getClass().getName());
    }

    public static void main1(String[] args) throws IOException {
        //创建configuration对象
        Configuration conf = new Configuration();
        //创建FileSystem对象
        //需求：查看hdfs集群服务器/user/zyh/passwd.txt的内容
        FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.1.132:9000/user/zyh/passwd.txt" ), conf);
        // args[0] hdfs://1.0.0.3:9000/user/zyh/passwd.txt
        // args[0] file:///etc/passwd.txt
        FSDataInputStream is = fs.open(new Path("hdfs://1.0.0.5:9000/user/zyh/passwd.txt" ));
        OutputStream os = new FileOutputStream(new File("D:/a.txt" ));
        byte[] buff = new byte[1024];
        int length = 0;
        while ((length = is.read(buff)) != -1) {
            System.out.println(new String(buff, 0, length));
            os.write(buff, 0, length);
            os.flush();
        }
        System.out.println(fs.getClass().getName());
        //这个是根据你传的变量来决定这个对象的实现类是哪个
    }
}